package com.linwen.javase.file;

import org.junit.Test;

import java.io.File;
import java.io.IOException;

/**
 * File是文件和文件夹的对象；
 * 对文件的读，写等操作需要配合IO流来完成
 * @author : linwen
 * @date : 下午10:24 2021/9/7
 */
public class FileTest {
    @Test
    public void test1() throws IOException {
        //三个构造方法
        File file = new File("hello.txt");
        File file1 = new File(file.getParent(), "haha.txt");
        if(!file.exists()){
            boolean newFile = file.createNewFile();
            if(newFile){
                System.out.println("文件创建成功: "+file.getName());
            }
        }
        if(!file1.exists()){
            boolean newFile = file1.createNewFile();
            if(newFile){
                System.out.println("文件创建成功: "+file1.getName());
            }
        }else{
            boolean delete = file1.delete();
            if(delete){
                System.out.println("文件删除成功："+file1.getName());
            }
        }
        // 常用方法
        System.out.println(file.getName());
        System.out.println(file.length());
        System.out.println(file.isHidden());
        System.out.println(file.canRead());
        System.out.println(file.canWrite());
        System.out.println(file.getAbsolutePath());
        System.out.println(file.getAbsoluteFile());
        System.out.println(file.isDirectory());
        System.out.println(file.isFile());
    }
}
